package com.base.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.common.core.service.UserService;
import com.base.system.domain.bo.TSysUserBo;
import com.base.system.domain.vo.TSysUserVo;
import com.base.system.mapper.TSysUserMapper;
import com.base.system.service.ITSysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.system.domain.TSysUser;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户Service业务层处理
 *
 * @author base
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class TSysUserServiceImpl implements ITSysUserService, UserService {

    private final TSysUserMapper baseMapper;

    /**
     * 查询用户
     */
    @Override
    public TSysUserVo queryById(String autoId){
        return baseMapper.selectVoById(autoId);
    }

    /**
     * 查询用户列表
     */
    @Override
    public TableDataInfo<TSysUserVo> queryPageList(TSysUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysUser> lqw = buildQueryWrapper(bo);
        Page<TSysUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户列表
     */
    @Override
    public List<TSysUserVo> queryList(TSysUserBo bo) {
        LambdaQueryWrapper<TSysUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysUser> buildQueryWrapper(TSysUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), TSysUser::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TSysUser::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getUkSerialId()), TSysUser::getUkSerialId, bo.getUkSerialId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TSysUser::getOperator, bo.getOperator());
        return lqw;
    }

    /**
     * 新增用户
     */
    @Override
    public Boolean insertByBo(TSysUserBo bo) {
        TSysUser add = BeanUtil.toBean(bo, TSysUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAutoId(add.getAutoId());
        }
        return flag;
    }

    /**
     * 修改用户
     */
    @Override
    public Boolean updateByBo(TSysUserBo bo) {
        TSysUser update = BeanUtil.toBean(bo, TSysUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String selectUserNameById(String userId) {
        TSysUserVo tSysUserVo = queryById(userId);
        return tSysUserVo == null? "":tSysUserVo.getUserName();
    }
}
